(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//时间转换
	function getMyDate(str){  
		var oDate = new Date(str),  
		oYear = oDate.getFullYear(),  
		oMonth = oDate.getMonth()+1,  
		oDay = oDate.getDate(),  
		oHour = oDate.getHours(),  
		oMin = oDate.getMinutes(),  
		oSen = oDate.getSeconds(),  
		oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
		return oTime;  
	}; 
	function getzf(num){  
          if(parseInt(num) < 10){  
              num = '0'+num;  
          }  
          return num;  
    }
	//获取列表
	function getlist(curpage,pagenum){
		var notifyType=$("#notifyType").val();
		var content=$("#content").val();
		var beginDate=$("#datepicker").val();
		var endDate=$("#datepicker2").val();
		
	   $.ajax({
			url:'http://web.icecn.net/internal/message/history/list.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				notifyType:notifyType,
				content:content,
				beginDate:beginDate,
				endDate:endDate,
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
						for(var i=0;i<dataset.length;i++){
							  dataset[i].beginTime= getMyDate(dataset[i].beginTime);
						   }
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
				curpage=1
			  	getlist(curpage,pagenum)
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();